package com.haidechizi.longpolling.server.persistent;

import com.alibaba.fastjson.JSON;
import com.haidechizi.longpolling.server.entity.ConfigData;
import com.haidechizi.longpolling.utils.EncryptUtils;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wangdejun
 * @create 2021/2/24 13:29
 */
public class ConfigDataPersistent {

    private ConfigDataPersistent() {
    }

    private static final Map<String, ConfigData> configDataHolder = new ConcurrentHashMap<>();

    public static final ConfigData getConfigData(String appCode) {
        return configDataHolder.get(appCode);
    }

    public static final void addConfigData(ConfigData configData) {
        String md5 = EncryptUtils.getMD5Str(JSON.toJSONString(configData.getDataMap()));
        configData.setMd5(md5);
        configDataHolder.put(configData.getAppCode(), configData);
    }

    public static final Collection<ConfigData> configDataList() {
        return configDataHolder.values();
    }
}
